home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.10 / terrain.h < prev   
Encoding:
C/C++ Source or Header  |  2006-07-12  |  2.7 KB  |  125 lines

  1. #ifndef _TERRAIN_
  2. #define _TERRAIN_
  3.  
  4. #include <d3dx9.h>
  5. #include <vector>
  6. #include <fstream>
  7. #include "heightmap.h"
  8. #include "debug.h"
  9. #include "shader.h"
  10. #include "object.h"
  11.  
  12. class CAMERA;
  13.  
  14. struct TERRAINVertex
  15. {
  16.     TERRAINVertex(){}
  17.     TERRAINVertex(D3DXVECTOR3 pos, D3DXVECTOR3 norm, D3DXVECTOR2 _uv1, D3DXVECTOR2 _uv2)
  18.     {
  19.         position = pos;
  20.         normal = norm;
  21.         uv1 = _uv1;
  22.         uv2 = _uv2;
  23.     }
  24.  
  25.     D3DXVECTOR3 position, normal;
  26.     D3DXVECTOR2 uv1, uv2;
  27.  
  28.     static const DWORD FVF;
  29. };
  30.  
  31. struct PATCH{
  32.     PATCH();
  33.     ~PATCH();
  34.     void Release();
  35.     HRESULT CreateMesh(TERRAIN &ter, RECT source, IDirect3DDevice9* Dev);
  36.     void Render();
  37.  
  38.     IDirect3DDevice9* m_pDevice;
  39.     ID3DXMesh *m_pMesh;
  40.     RECT m_mapRect;
  41.     BBOX m_BBox;
  42. };
  43.  
  44. struct MAPTILE{
  45.     MAPTILE()    //Set everything to 0
  46.     {
  47.         m_type = m_set = 0; 
  48.         m_height = m_cost = 0.0f;
  49.         m_walkable = false;
  50.         m_pParent = NULL;
  51.  
  52.         for(int i=0;i<8;i++)
  53.             m_pNeighbors[i] = NULL;
  54.     }
  55.  
  56.     int m_type, m_set;
  57.     float m_height, m_cost;
  58.     bool m_walkable;
  59.     MAPTILE* m_pNeighbors[8];
  60.  
  61.     // Pathfinding variables
  62.     INTPOINT m_mappos;
  63.     float f,g;
  64.     bool open, closed;
  65.     MAPTILE *m_pParent;
  66. };
  67.  
  68. class TERRAIN{
  69.     friend struct PATCH;
  70.     friend class MOUSE;
  71.     friend class CAMERA;
  72.     friend class APPLICATION;
  73.     public:
  74.         TERRAIN();        
  75.         void Init(IDirect3DDevice9* Dev, INTPOINT _size);
  76.         void Release();
  77.         void GenerateRandomTerrain(int numPatches);
  78.         void CreatePatches(int numPatches);
  79.         void CalculateAlphaMaps();
  80.         void CalculateLightMap();
  81.         D3DXVECTOR3 GetNormal(int x, int y);
  82.         void AddObject(int type, INTPOINT mappos);
  83.         void Render(CAMERA &camera);        
  84.         void Progress(std::string text, float prc);
  85.  
  86.         //Pathfinding
  87.         bool Within(INTPOINT p);    //Test if a point is within the bounds of the terrain
  88.         void InitPathfinding();
  89.         void CreateTileSets();
  90.         std::vector<INTPOINT> GetPath(INTPOINT start, INTPOINT goal);
  91.         MAPTILE* GetTile(int x, int y);
  92.         MAPTILE* GetTile(INTPOINT p){return GetTile(p.x, p.y);}
  93.         D3DXVECTOR3 GetWorldPos(INTPOINT mappos);
  94.  
  95.         //Save and Load Map
  96.         void SaveTerrain(char fileName[]);
  97.         void LoadTerrain(char fileName[]);
  98.  
  99.         //Public variables
  100.         MAPTILE *m_pMapTiles;
  101.  
  102.     private:
  103.  
  104.         INTPOINT m_size;
  105.         IDirect3DDevice9* m_pDevice; 
  106.         ID3DXFont *m_pProgressFont;
  107.  
  108.         HEIGHTMAP *m_pHeightMap;
  109.         std::vector<PATCH*> m_patches;
  110.         std::vector<IDirect3DTexture9*> m_diffuseMaps;
  111.         std::vector<OBJECT> m_objects;
  112.         IDirect3DTexture9* m_pAlphaMap;
  113.         IDirect3DTexture9* m_pLightMap;
  114.  
  115.         SHADER m_terrainPS, m_terrainVS;
  116.         SHADER m_objectPS, m_objectVS;
  117.  
  118.         D3DXVECTOR3 m_dirToSun;
  119.         D3DXHANDLE m_vsMatW, m_vsMatVP, m_vsDirToSun;
  120.         D3DXHANDLE m_objMatW, m_objMatVP, m_objDirToSun, m_objMapSize;
  121.  
  122.         D3DMATERIAL9 m_mtrl;
  123. };
  124.  
  125. #endif